package ex1;

public class Counter {
	private int backCount;
	
	/**
	 * Counter constructor
	 * @param _backCount
	 */
	public Counter (int _backCount) {
		if (_backCount < 0) {
			_backCount = 0;
		}
		backCount = _backCount;
	}
	
	/**
	 * Wait until done
	 * waits on the counter until backCount is zero
	 */
	public synchronized void waitUntilDone() {
		while (backCount != 0) {
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
	}
	
	/**
	 * Reduces backCount by 1 and notifies if is done
	 */
	public synchronized void tick() {
		backCount--;
		notifyAll();	
	}
}
